package com.mbc.common.manager;

import java.util.ArrayList;
import java.util.Date;

import com.mbc.common.CommonConstants;
import com.mbc.common.dao.ApplicationItemDAO;
import com.mbc.common.dao.BranchDAO;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.SystemPropertyDAO;
import com.mbc.common.dao.SystemPropertyDAOImpls;
import com.mbc.common.dao.UserDAO;
import com.mbc.common.data.UserData;
import com.mbc.common.entities.ApplicationItem;
import com.mbc.common.entities.Branch;
import com.mbc.common.entities.SystemProperty;
import com.mbc.common.entities.User;
import com.mbc.common.exception.UnauthorizedAccessException;

public class SystemMgr extends AbstractMgr {
	
	public SystemMgr()
	{
		
	}
	
	public SystemMgr(String sessionId)
	{
		this.sessionId = sessionId;
	}
	
	public ArrayList<ApplicationItem> getAllAppItem()throws Exception{
		openConnection();
		try {
			ApplicationItemDAO l_AppItemDAO = CommonDAOFactory.getApplicationItemDAO(dbManager.getConnection());
			return l_AppItemDAO.findAll();
		} catch (Exception e) {
			throw e;
		}
		finally{
			dbManager.closeConnection();
		}
	}
	
	public UserData login(String loginId, String password) throws Exception
	{
		UserData l_userData= new UserData();
		openConnection();
		try {
			UserDAO l_UserDAO = CommonDAOFactory.getUserDAO(dbManager.getConnection());
			
			User l_user =  l_UserDAO.findByLoginId(loginId);
			if(l_user != null){
				if(l_user.getPassword().equals(password)){
					if(l_user.getStatus() == CommonConstants.RecordStatus.delete)
						throw new UnauthorizedAccessException("User was deleted.");
//					if(l_user.getStatus() == UserMgr.USER_ONLINE)
//						throw new LockedUserException("User " + loginId + " is locked.");
					BranchDAO l_OfficeDAO = CommonDAOFactory.getBranchDAO(dbManager.getConnection());
					ApplicationItemDAO l_AppItemDAO  = CommonDAOFactory.getApplicationItemDAO(dbManager.getConnection());
					Branch l_Branch = l_OfficeDAO.findByUserId(l_user.getUserId());
					l_userData.setLoginID(l_user.getLoginId());
					l_userData.setUserName(l_user.getUserName());
					l_userData.setUserID(l_user.getUserId());
					l_userData.setBranchCode(l_Branch.getBranchCode());
					l_userData.setBranchId(l_Branch.getBranchId());
					l_userData.setBranchName(l_Branch.getName());
					l_userData.setCountryCode(l_Branch.getCountry());
					l_userData.setAppItemList(l_AppItemDAO.findByUserId(l_user.getUserId()));
					l_UserDAO.updateUserOnlineStatus(l_user.getUserId(), UserMgr.USER_ONLINE);
				}
				else
					throw new UnauthorizedAccessException("Invalid password.");
			}
			else
				throw new UnauthorizedAccessException("Invalid Login Id.");
		}
		catch (Exception e) {
			throw e;
		}
		finally{
			dbManager.closeConnection();
		}
		return l_userData;
	}
	
	public SystemProperty getSystemProperty(String key)throws Exception{
		openConnection();
		try {
			SystemPropertyDAO l_SysPropertyDAO = new SystemPropertyDAOImpls(getConnection());
			return l_SysPropertyDAO.getSystemProperty(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void updateLatestLoginDateTime(Date latestLoginDateTime, long userId)throws Exception{
		openConnection();
		try {
			UserDAO l_userDAO = CommonDAOFactory.getUserDAO(getConnection());
			l_userDAO.updateLatestLoginDateTime(latestLoginDateTime, userId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
}
